﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data.SqlClient;
using System.Data;
using System.Text;

namespace CMS
{
    public partial class guanli : System.Web.UI.Page
    {
        public SqlHelper sqlHelper = new SqlHelper();

        public FenYe fenye = new FenYe();

        string url = "guanli.aspx";

        public SqlDataReader dr = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            //获取用户名
            if (Session["tea_no"] != null)
            {
                this.stu_no.InnerText = Session["tea_no"].ToString();
            }
            //else
            //{
            //    Response.Redirect("log.aspx");
            //}
            if (Session["tea_name"] != null)
            {
                this.stu_name.InnerText = Session["tea_name"].ToString();
            }

            if (Request.QueryString["pageNow"] != null)
            {
                fenye.pageNow = int.Parse(Request.QueryString["pageNow"].ToString());
            }

            if (Request.QueryString["StuNo"] != null && Request.QueryString["type"]=="del")
            {
                if (this.del(Request.QueryString["StuNo"]) > 0) {
                    Response.Write("<script>alert('删除成功')</script>");
                }
            }

            if (Request.QueryString["StuNo"] != null && Request.QueryString["type"] == "update")
            {
                if (this.update(Request.QueryString["StuNo"]) > 0)
                {
                    Response.Write("<script>alert('修改成功')</script>");
                }
            }

            fenye.pageSize = 7;

            string pageNow = fenye.pageNow.ToString();
            string pageSize = fenye.pageSize.ToString();
            string notFound = ((fenye.pageNow - 1) * fenye.pageSize).ToString();

            string sql1 = "SELECT   COUNT(*) FROM student";
            StringBuilder sb = new StringBuilder(1024);
            sb.AppendFormat("SELECT   TOP ({0}) * ", pageSize);
            sb.Append("FROM      student ");
            sb.Append("WHERE   (StuNo NOT IN ");
            sb.AppendFormat("                    (SELECT   TOP ({0}) StuNo ", notFound);
            sb.Append("                     FROM      student AS student_1))");

            sqlHelper.fenye_query(sql1, sb.ToString(), url, fenye);

            dr = fenye.dr;

            //if (dr.Read()) {
            //    Response.Write(dr["StuNo"]);
            //}
        }

        public int del(string stuno) { 
            string sql = "DELETE FROM student WHERE   StuNo = N'"+stuno+"'";
            return sqlHelper.ExecuteNonQuery(sql);
        }

        public int update(string stuno) {
            return 1;
        }
    }
}